﻿using Coldairarrow.Business.Base;
using Coldairarrow.Entity.Base;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Util;
using LinqKit;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Api.Controllers.Base
{
    [Route("/Base/[controller]/[action]")]
    public class Base_UserDepartmentController : BaseApiController
    {
        #region DI

        public Base_UserDepartmentController(IBase_UserDepartmentBusiness base_UserDepartmentBus)
        {
            _base_UserDepartmentBus = base_UserDepartmentBus;
        }

        IBase_UserDepartmentBusiness _base_UserDepartmentBus { get; }

        #endregion

        #region 获取

        [HttpPost]
        public async Task<PageResult<Basr_UserDeptDTO>> GetDataList(PageInput<ConditionDTO> input)
        {
            return await _base_UserDepartmentBus.GetDataListAsync(input);
        }
        /// <summary>
        /// 根据用户id查询 所对应的组织机构
        /// </summary>
        [HttpPost]
        public async Task<List<Basr_UserDeptDTO>> GetDataListByUserid(string UserId)
        {
            var res = await _base_UserDepartmentBus.GetDataListByAsync(UserId);
            return res;
        }
        /// <summary>
        /// 根据组织id查找用户
        /// </summary>
        /// <param name="deptid"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<Basr_UserDeptDTO>> GetDataListByDeptuserid(string deptid)
        {
            var res = await _base_UserDepartmentBus.GetDataListByAsyncDeptid(deptid);
            return res;
        }
        [HttpPost]
        public async Task<Base_UserDepartment> GetTheData(IdInputDTO input)
        {
            return await _base_UserDepartmentBus.GetTheDataAsync(input.id);
        }

        #endregion

        #region 提交

        [HttpPost]
        public async Task SaveData(Base_UserDepartmentDTO data)
        {
            //先删除原有的lineid的所有数据
            var where = LinqHelper.True<Base_UserDepartment>();
            if (!data.UserID.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<Base_UserDepartment, bool>(
                        ParsingConfig.Default, false, $@"UserID.Equals(@0)", data.UserID);
                where = where.And(newWhere);
            }
            await _base_UserDepartmentBus.DeleteOtherIdDataAsync(where);
            //批量插入新的数据
            List<Base_UserDepartment> models = new List<Base_UserDepartment>();
            foreach (var item in data.DepartmentID)
            {
                Base_UserDepartment base_User = new Base_UserDepartment();
                InitEntity(data);
                base_User.Id = data.Id;
                base_User.UserID = data.UserID;
                base_User.DepartmentID = item.ToString();
                models.Add(base_User);
            }
            await _base_UserDepartmentBus.AddDataAsync(models);
            //更新缓存
            await _base_UserDepartmentBus.UpdateCacheAsync(data);
        }


        [HttpPost]
        public async Task DeleteData(List<string> ids)
        {
            await _base_UserDepartmentBus.DeleteDataAsync(ids);
        }

        #endregion
    }
}